package com.square.service.redis.provider.controller.rpc;

import com.baomidou.mybatisplus.extension.api.R;
import com.square.service.redis.provider.service.RedisService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author : 18.40
 * @date : 2020/4/5 10:28
 * @description : redis操作控制层（时间单位：秒）
 */
@RestController
@RequestMapping("/rpc")
public class RedisRPC {

    @Resource
    private RedisService redisService;

    /**
     * 指定缓存失效时间
     */
    @PostMapping("/setExpire")
    public R<?> setExpire(@RequestParam("key") String key, @RequestParam("time") long time) {
        return R.ok(redisService.setExpire(key, time));
    }

    /**
     * 根据key 获取过期时间
     */
    @PostMapping("/getExpire")
    public R<?> getExpire(@RequestParam("key") String key) {
        return R.ok(redisService.getExpire(key));
    }

    /**
     * 判断key是否存在
     */
    @PostMapping("/hasKey")
    public R<?> hasKey(@RequestParam("key") String key) {
        return R.ok(redisService.hasKey(key));
    }

    /**
     * 删除缓存
     */
    @PostMapping("/delete")
    public R<?> delete(@RequestParam("key") String key) {
        redisService.delete(key);
        return R.ok(null);
    }

    /**
     * 普通缓存获取
     */
    @PostMapping("/get")
    public R<?> get(@RequestParam("key") String key) {
        return R.ok(redisService.get(key));
    }

    /**
     * 普通缓存放入
     */
    @PostMapping("/set")
    public R<?> set(@RequestParam("key") String key, @RequestParam("value") Object value) {
        return R.ok(redisService.set(key, value));
    }

    /**
     * 普通缓存放入并设置时间
     */
    @PostMapping("/setWithTime")
    public R<?> set(@RequestParam("key") String key, @RequestParam("value") Object value, @RequestParam("time") long time) {
        return R.ok(redisService.set(key, value, time));
    }

    /**
     * 递增
     */
    @PostMapping("/increment")
    public R<?> increment(@RequestParam("key") String key, @RequestParam("delta") long delta) {
        return R.ok(redisService.increment(key, delta));
    }

    /**
     * 递减
     */
    @PostMapping("/decreasing")
    public R<?> decreasing(@RequestParam("key") String key, @RequestParam("delta") long delta) {
        return R.ok(redisService.decreasing(key, delta));
    }

    /**
     * HashGet
     */
    @PostMapping("/getHashMap")
    public R<?> getHashMap(@RequestParam("key") String key, @RequestParam("item") String item) {
        return R.ok(redisService.getHashMap(key, item));
    }

    /**
     * 获取hashKey对应的所有键值
     */
    @PostMapping("/getHashMapAllKey")
    public R<?> getHashMap(@RequestParam("key") String key) {
        return R.ok(redisService.getHashMap(key));
    }

    /**
     * HashSet
     */
    @PostMapping("/setHashMap")
    public R<?> setHashMap(@RequestParam("key") String key, @RequestParam("map") Map<String, Object> map) {
        return R.ok(redisService.setHashMap(key, map));
    }

    /**
     * HashSet 并设置时间
     */
    @PostMapping("/setHashMapWithTime")
    public R<?> setHashMap(@RequestParam("key") String key, @RequestParam("map") Map<String, Object> map, @RequestParam("time") long time) {
        return R.ok(redisService.setHashMap(key, map, time));
    }

    /**
     * 向一张hash表中放入数据,如果不存在将创建
     */
    @PostMapping("/setHashMapSingle")
    public R<?> setHashMap(@RequestParam("key") String key, @RequestParam("item") String item, @RequestParam("value") Object value) {
        return R.ok(redisService.setHashMap(key, item, value));
    }

    /**
     * 向一张hash表中放入数据,如果不存在将创建
     */
    @PostMapping("/setHashMapSingleWithTime")
    public R<?> setHashMap(@RequestParam("key") String key, @RequestParam("item") String item, @RequestParam("value") Object value, @RequestParam("time") long time) {
        return R.ok(redisService.setHashMap(key, item, value, time));
    }

    /**
     * 删除hash表中的值
     */
    @PostMapping("/deleteHashMap")
    public R<?> deleteHashMap(@RequestParam("key") String key, @RequestParam("item") Object... item) {
        redisService.deleteHashMap(key, item);
        return R.ok(null);
    }

    /**
     * 判断hash表中是否有该项的值
     */
    @PostMapping("/hasKeyForHashMap")
    public R<?> hasKeyForHashMap(@RequestParam("key") String key, @RequestParam("item") String item) {
        return R.ok(redisService.hasKeyForHashMap(key, item));
    }

    /**
     * hash递增 如果不存在,就会创建一个 并把新增后的值返回
     */
    @PostMapping("/incrementForHashMap")
    public R<?> incrementForHashMap(@RequestParam("key") String key, @RequestParam("item") String item, @RequestParam("by") double by) {
        return R.ok(redisService.incrementForHashMap(key, item, by));
    }

    /**
     * hash递减
     */
    @PostMapping("/decreasingForHashMap")
    public R<?> decreasingForHashMap(@RequestParam("key") String key, @RequestParam("item") String item, @RequestParam("by") double by) {
        return R.ok(redisService.decreasingForHashMap(key, item, by));
    }

    /**
     * 根据key获取Set中的所有值
     */
    @PostMapping("/getSet")
    public R<?> getSet(@RequestParam("key") String key) {
        return R.ok(redisService.getSet(key));
    }

    /**
     * 根据value从一个set中查询,是否存在
     */
    @PostMapping("/hasKeyForSet")
    public R<?> hasKeyForSet(@RequestParam("key") String key, @RequestParam("value") Object value) {
        return R.ok(redisService.hasKeyForSet(key, value));
    }

    /**
     * 将数据放入set缓存
     */
    @PostMapping("/setSet")
    public R<?> setSet(@RequestParam("key") String key, @RequestParam("values") Object... values) {
        return R.ok(redisService.setSet(key, values));
    }

    /**
     * 将set数据放入缓存
     */
    @PostMapping("/setSetWithTime")
    public R<?> setSet(@RequestParam("key") String key, @RequestParam("time") long time, @RequestParam("values") Object... values) {
        return R.ok(redisService.setSet(key, time, values));
    }

    /**
     * 获取set缓存的长度
     */
    @PostMapping("/getSetSize")
    public R<?> getSetSize(@RequestParam("key") String key) {
        return R.ok(redisService.getSetSize(key));
    }

    /**
     * 移除值为value的
     */
    @PostMapping("/removeSet")
    public R<?> removeSet(@RequestParam("key") String key, @RequestParam("values") Object... values) {
        return R.ok(redisService.removeSet(key, values));
    }

    /**
     * 获取list缓存的内容
     */
    @PostMapping("/getList")
    public R<?> getList(@RequestParam("key") String key, @RequestParam("start") long start, @RequestParam("end") long end) {
        return R.ok(redisService.getList(key, start, end));
    }

    /**
     * 获取list缓存的长度
     */
    @PostMapping("/getListSize")
    public R<?> getListSize(@RequestParam("key") String key) {
        return R.ok(redisService.getListSize(key));
    }

    /**
     * 通过索引 获取list中的值
     */
    @PostMapping("/getListObjByIndex")
    public R<?> getListObjByIndex(@RequestParam("key") String key, @RequestParam("index") long index) {
        return R.ok(redisService.getListObjByIndex(key, index));
    }

    /**
     * 将list放入缓存
     */
    @PostMapping("/setList")
    public R<?> setList(@RequestParam("key") String key, @RequestParam("value") Object value) {
        return R.ok(redisService.setList(key, value));
    }

    /**
     * 将list放入缓存
     */
    @PostMapping("/setListWithTime")
    public R<?> setList(@RequestParam("key") String key, @RequestParam("value") Object value, @RequestParam("time") long time) {
        return R.ok(redisService.setList(key, value, time));
    }

    /**
     * 将list放入缓存
     */
    @PostMapping("/setListGeneric")
    public R<?> setList(@RequestParam("key") String key, @RequestParam("value") List<Object> value) {
        return R.ok(redisService.setList(key, value));
    }

    /**
     * 将list放入缓存
     */
    @PostMapping("/setListGenericWithTime")
    public R<?> setList(@RequestParam("key") String key, @RequestParam("value") List<Object> value, @RequestParam("time") long time) {
        return R.ok(redisService.setList(key, value, time));
    }

    /**
     * 根据索引修改list中的某条数据
     */
    @PostMapping("/updateListObjByIndex")
    public R<?> updateListObjByIndex(@RequestParam("key") String key, @RequestParam("index") long index, @RequestParam("value") Object value) {
        return R.ok(redisService.updateListObjByIndex(key, index, value));
    }

    /**
     * 移除N个值为value
     */
    @PostMapping("/removeList")
    public R<?> removeList(@RequestParam("key") String key, @RequestParam("count") long count, @RequestParam("value") Object value) {
        return R.ok(redisService.removeList(key, count, value));
    }

    /**
     * 添加一个元素, zset与set最大的区别就是每个元素都有一个score，因此有个排序的辅助功能;  zadd
     */
    @PostMapping("/setZSet")
    public R<?> setZSet(@RequestParam("key") String key, @RequestParam("value") Object value, @RequestParam("score") double score) {
        return R.ok(redisService.setZSet(key, value, score));
    }

    /**
     * 删除元素 zrem
     */
    @PostMapping("/removeZSet")
    public R<?> removeZSet(@RequestParam("key") String key, @RequestParam("value") String value) {
        redisService.removeZSet(key, value);
        return R.ok(null);
    }

    /**
     * 递增
     */
    @PostMapping("/incrementForZSet")
    public R<?> incrementForZSet(@RequestParam("key") String key, @RequestParam("value") String value, @RequestParam("score") double score) {
        return R.ok(redisService.incrementForZSet(key, value, score));
    }

    /**
     * 查询value对应的score   zscore
     */
    @PostMapping("/getZSetScore")
    public R<?> getZSetScore(@RequestParam("key") String key, @RequestParam("value") String value) {
        return R.ok(redisService.getZSetScore(key, value));
    }

    /**
     * 判断value在zset中的排名  zrank
     */
    @PostMapping("/getZSetRank")
    public R<?> getZSetRank(@RequestParam("key") String key, @RequestParam("value") String value) {
        return R.ok(redisService.getZSetRank(key, value));
    }

    /**
     * 返回集合的长度
     */
    @PostMapping("/getZSetSize")
    public R<?> getZSetSize(@RequestParam("key") String key) {
        return R.ok(redisService.getZSetSize(key));
    }


    /**
     * 查询集合中指定顺序的值， 0 -1 表示获取全部的集合内容  zrange
     */
    @PostMapping("/getZSetRange")
    public R<?> getZSetRange(@RequestParam("key") String key, @RequestParam("start") int start, @RequestParam("end") int end) {
        return R.ok(redisService.getZSetRange(key, start, end));
    }

}
